package n08.second;

import java.util.Arrays;
import java.util.Comparator;

public class N01 {
	public static Comparator<Student> sortBy(Student.SortBy by){
		return new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				switch(by) {
					case Age: return o1.getAge()-o2.getAge();
					case Height: return Double.compare(o1.getHeight(), o2.getHeight());
					case StudentId: return o1.getStudentId().compareTo(o2.getStudentId());
					default: return 0;
				}
			}
		};
	}
	public static void main(String[] args) {
		Student[]a= {
				new Student("20240108", "张三", 21, 1.75),
				new Student("20240105", "李四", 19, 1.95),
				new Student("20240101", "王五", 20, 1.85),
		};
		System.out.println("按年龄的升序排列Student数组：");
		Arrays.sort(a, sortBy(Student.SortBy.Age));
		for (Student student : a) {
			System.out.println(student);
		}
		System.out.println("按年龄的降序排列Student数组：");
		Arrays.sort(a, sortBy(Student.SortBy.Age).reversed());
		for (Student student : a) {
			System.out.println(student);
		}
	}

}
